"""Make figure showing yearly rate of new novel publications.

When numbers are known, they are shown. When they are not known, model estimates
are shown.

"""
import argparse
import hashlib
import os
import pickle

import matplotlib.pyplot as plt
import matplotlib.style
import matplotlib.ticker
import numpy as np
import pandas as pd
import scipy.special

import datasets
import inference
import make_table_novels_by_year  # function called likely belongs in datasets
import plot_common

parser = argparse.ArgumentParser()
parser.add_argument('output_filename', type=str, help='Output path for figure.')

# use seaborn style globally
matplotlib.style.use('seaborn-deep')

def make_plot(output_filename):
    gender = 'men'
    assert gender in {'men', 'women', 'unknown'}, gender
    df = make_table_novels_by_year.dataset_years()
    fig = plt.figure()  # noqa
    ax = plt.gca()
    assert len(df) == (1919 - 1789 + 1)
    for i, year in enumerate(range(1789, 1799 + 1)):
        p05, p25, p50, p75, p95 = df.loc[year, [f'y_{gender}_sim_pct_{p}' for p in ('p05', 'p25', 'p50', 'p75', 'p95')]]
        width = plot_common.bar_width
        ax.fill_between([year - width / 8, year + width / 8], p05, p95, alpha=0.7, color='k')
        ax.fill_between([year - width / 2, year + width / 2], p25, p75, alpha=0.7, color='k')
    # known exactly for these 30 years
    for i, year in enumerate(range(1800, 1829 + 1)):
        column_lookup = {'men': 'male', 'women': 'female', 'unknown': 'unknown'}
        pct = 100.0 * df.loc[year, f'novels_{column_lookup[gender]}'] / df.loc[year, 'novels']
        ax.plot(year, pct, marker='.', markersize=3, alpha=0.8, color='black')
    for i, year in enumerate(range(1830, 1919 + 1)):
        p05, p25, p50, p75, p95 = df.loc[year, [f'men_of_known_pct_sim_{p}' for p in ('p05', 'p25', 'p50', 'p75', 'p95')]]
        width = plot_common.bar_width
        ax.fill_between([year - width / 8, year + width / 8], p05, p95, alpha=0.7, color='k')
        ax.fill_between([year - width / 2, year + width / 2], p25, p75, alpha=0.7, color='k')
    # title added to caption
    ax.xaxis.set_minor_locator(matplotlib.ticker.AutoMinorLocator())
    ax.yaxis.set_minor_locator(matplotlib.ticker.AutoMinorLocator())
    plt.ylim(0, 85)
    pos, _ = plt.yticks()
    plt.yticks(pos, [f'{int(y):d}%' for y in pos])
    plt.tight_layout()
    plt.savefig(output_filename)
    print('saved plot to file:', output_filename)


if __name__ == '__main__':
    args = parser.parse_args()
    make_plot(args.output_filename)
